#!/bin/bash

# 一键安装zsh和oh-my-zsh脚本
# 包含常用插件的自动配置

echo "开始安装zsh和oh-my-zsh..."

# 检查是否已安装zsh
if ! command -v zsh &> /dev/null; then
    echo "zsh未安装，正在安装zsh..."
    if command -v apt &> /dev/null; then
        sudo apt update && sudo apt install -y zsh
    elif command -v yum &> /dev/null; then
        sudo yum install -y zsh
    elif command -v dnf &> /dev/null; then
        sudo dnf install -y zsh
    elif command -v pacman &> /dev/null; then
        sudo pacman -S zsh
    else
        echo "无法自动安装zsh，请手动安装后再运行此脚本"
        exit 1
    fi
fi

# 下载并修改oh-my-zsh安装脚本
echo "下载oh-my-zsh安装脚本..."
wget https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh

echo "修改安装脚本使用国内镜像..."
sed -i 's|REPO=${REPO:-ohmyzsh/ohmyzsh}|REPO=${REPO:-mirrors/oh-my-zsh}|' install.sh
sed -i 's|REMOTE=${REMOTE:-https://github.com/${REPO}.git}|REMOTE=${REMOTE:-https://gitee.com/${REPO}.git}|' install.sh

# 设置环境变量以非交互模式安装
export RUNZSH=no
export KEEP_ZSHRC=yes

echo "安装oh-my-zsh..."
sh install.sh

# 清理安装脚本
rm -f install.sh

echo "下载zsh插件..."

# 下载自动补全插件
echo "下载 zsh-autosuggestions 插件..."
git clone https://github.starringshop.top/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

# 下载历史搜索插件
echo "下载 zsh-history-substring-search 插件..."
git clone https://github.starringshop.top/https://github.com/zsh-users/zsh-history-substring-search ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-history-substring-search

# 下载语法高亮插件
echo "下载 zsh-syntax-highlighting 插件..."
git clone https://github.starringshop.top/https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

echo "配置zsh插件..."

# 备份原始.zshrc文件
if [ -f ~/.zshrc ]; then
    cp ~/.zshrc ~/.zshrc.backup.$(date +%Y%m%d_%H%M%S)
    echo "已备份原始.zshrc文件"
fi

# 修改.zshrc文件中的plugins配置
if [ -f ~/.zshrc ]; then
    # 使用sed替换plugins行
    sed -i 's/^plugins=([^)]*)/plugins=(git zsh-autosuggestions zsh-history-substring-search zsh-syntax-highlighting)/' ~/.zshrc
    echo "已更新插件配置"
else
    echo "警告: ~/.zshrc 文件不存在"
fi

echo "设置zsh主题为ys..."
# 直接修改.zshrc文件设置主题为ys
if [ -f ~/.zshrc ]; then
    sed -i 's/^ZSH_THEME="[^"]*"/ZSH_THEME="ys"/' ~/.zshrc
    echo "已设置主题为ys"
else
    echo "警告: ~/.zshrc 文件不存在，无法设置主题"
fi

echo "设置zsh为默认shell..."
# 检查当前shell是否已是zsh
if [ "$SHELL" != "$(which zsh)" ]; then
    chsh -s $(which zsh)
    echo "已设置zsh为默认shell，请重新登录或重启终端以生效"
else
    echo "zsh已是默认shell"
fi

echo ""
echo "============================================"
echo "安装完成！"
echo "============================================"
echo ""
echo "已安装的插件："
echo "  - git (默认)"
echo "  - zsh-autosuggestions (自动补全建议)"
echo "  - zsh-history-substring-search (历史搜索)"
echo "  - zsh-syntax-highlighting (语法高亮)"
echo ""
echo "使用方法："
echo "  - 重新启动终端或运行 'zsh' 命令"
echo "  - 或者运行 'source ~/.zshrc' 重新加载配置"
echo ""
echo "插件快捷键："
echo "  - Tab: 接受自动补全建议"
echo "  - ↑/↓: 搜索历史命令"
echo "  - Ctrl+R: 反向搜索历史"
echo ""

# 询问是否立即切换到zsh
read -p "是否现在就切换到zsh? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    exec zsh
fi